﻿using System;
using System.Web;
using Ext.Net;
using System.Collections.Generic;
using System.Collections;
using Film.Repository;

namespace PhuongDoAn.Admin.UserAccount.handler
{
    /// <summary>
    /// Summary description for ListHandler
    /// </summary>
    public class ListHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string query = "";
            int start = 0;
            int limit = 25;
            int count = 0;

            if (!string.IsNullOrEmpty(context.Request["query"]))
            {
                query = context.Request["query"];
            }
            if (!string.IsNullOrEmpty(context.Request["start"]))
            {
                int.TryParse(context.Request["start"], out start);
            }

            if (!string.IsNullOrEmpty(context.Request["limit"]))
            {
                int.TryParse(context.Request["limit"], out limit);
            }

            string webuser = "";
            
            if (!string.IsNullOrEmpty(context.Request["userweb"]))
            {
                webuser = context.Request["userweb"].Trim();
            }

            List<tblUserAccount> result = new List<tblUserAccount>();
            tblUserAccountRepository userAccountRepository = new tblUserAccountRepository();
            result = userAccountRepository.Filter(query, start, limit, out count, webuser=="");

            List<tblUserAccountHandler> listHandler = result.ConvertAll<tblUserAccountHandler>(delegate(tblUserAccount e)
            {
                return new tblUserAccountHandler() { UserID = e.UserID , Email = e.Email, FullName = e.FullName, Status = e.Status, UserName = e.UserName };
            });

            context.Response.Write(string.Format("{{'Data':{0},'TotalRecords':{1}}}", JSON.Serialize(listHandler), count));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class tblUserAccountHandler
    {
        public int UserID {get;set;}
        public  string UserName {get;set;}
        public string FullName {get;set;}
        public string Email {get;set;}
        public bool Status {get;set;}
        
    }
}